package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallAdmin;
import com.cskaoyan.bean.CskaoyanmallLog;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.config.Logging;
import com.cskaoyan.mapper.CskaoyanmallAdminMapper;
import com.cskaoyan.mapper.CskaoyanmallLogMapper;
import com.cskaoyan.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author Fantasylkh
 */
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    CskaoyanmallAdminMapper adminMapper;

    @Autowired
    CskaoyanmallLogMapper logMapper;

    @Override
    public void login(BaseRespVo baseRespVo) {

        Date date = new Date();
        CskaoyanmallLog log = new CskaoyanmallLog();
        log.setAddTime(date);
        log.setUpdateTime(date);
        log.setDeleted(false);
        log.setAction("登录");
        log.setType(1);
        if (baseRespVo.getErrno() == 0) {
            String adminName = (String) SecurityUtils.getSubject().getPrincipal();

            CskaoyanmallAdmin admin = adminMapper.selectAdminByName(adminName);
            log.setAdmin(adminName);
            log.setStatus(true);
            log.setIp(admin.getLastLoginIp());
        } else {
            log.setAdmin("匿名用户");
            log.setIp("该方法走错了");
            log.setStatus(false);
            log.setResult("用户帐号或密码不正确");
        }
        logMapper.insert(log);
    }

    @Override
    public void loggin(BaseRespVo baseRespVo, JoinPoint joinPoint) {
        String adminName = (String) SecurityUtils.getSubject().getPrincipal();
        if (adminName == null || "".equals(adminName)) {
            return;
        }

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Logging logging = method.getAnnotation(Logging.class);

        CskaoyanmallLog log = new CskaoyanmallLog();
        Date date = new Date();
        log.setAddTime(date);
        log.setUpdateTime(date);
        log.setType(logging.type());
        log.setStatus(logging.status());
        log.setAction(logging.value());
        log.setComment(logging.comment());
        log.setDeleted(false);
        Object baseRespVoData = baseRespVo.getData();
        if (baseRespVoData instanceof CskaoyanmallAdmin) {
            log.setResult(((CskaoyanmallAdmin) baseRespVoData).getUsername());
        }
        if ("删除管理员".equals(logging.value())) {
            Object[] args = joinPoint.getArgs();
            for (Object arg : args) {
                if (arg instanceof CskaoyanmallAdmin) {
                    log.setResult(((CskaoyanmallAdmin) arg).getUsername());
                }
            }
        }

        CskaoyanmallAdmin admin = adminMapper.selectAdminByName(adminName);
        log.setIp(admin.getLastLoginIp());
        log.setAdmin(admin.getUsername());

        logMapper.insert(log);
    }

    @Override
    public void loginException(JoinPoint joinPoint, Exception exception) {
        Date date = new Date();
        CskaoyanmallLog log = new CskaoyanmallLog();
        log.setAddTime(date);
        log.setUpdateTime(date);
        log.setDeleted(false);
        log.setAction("登录");
        log.setType(1);
        log.setAdmin("匿名用户");
        log.setStatus(false);
        log.setResult(exception.getMessage());
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof HttpServletRequest){
                String remoteAddr = ((HttpServletRequest) arg).getRemoteAddr();
                log.setIp(remoteAddr);
            }
        }
        logMapper.insert(log);
    }

    @Override
    public void exception(JoinPoint joinPoint, Exception exception) {
        String adminName = (String) SecurityUtils.getSubject().getPrincipal();
        if (adminName == null || "".equals(adminName)) {
            return;
        }

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Logging logging = method.getAnnotation(Logging.class);

        CskaoyanmallLog log = new CskaoyanmallLog();
        Date date = new Date();
        log.setAddTime(date);
        log.setUpdateTime(date);
        log.setType(logging.type());
        log.setStatus(logging.status());
        log.setAction(logging.value());
        log.setComment(logging.comment());
        log.setResult(exception.getMessage());
        log.setDeleted(false);
        CskaoyanmallAdmin admin = adminMapper.selectAdminByName(adminName);
        log.setIp(admin.getLastLoginIp());
        log.setAdmin(admin.getUsername());

        logMapper.insert(log);
    }

    @Override
    public void logout(String principal) {
        Date date = new Date();
        CskaoyanmallLog log = new CskaoyanmallLog();
        log.setAddTime(date);
        log.setUpdateTime(date);
        log.setDeleted(false);
        log.setAction("登出");
        log.setType(1);
        CskaoyanmallAdmin admin = adminMapper.selectAdminByName(principal);
        if(!ObjectUtils.isEmpty(admin)){
            log.setAdmin(principal);
            log.setStatus(true);
            log.setIp(admin.getLastLoginIp());
            logMapper.insert(log);
        }

    }
}
